home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / JPNL Libraries / NetClient.java < prev    next >
Text File  |  1996-05-23  |  3KB  |  152 lines

  1. package au.com.peter.libraries;
  2.  
  3. import java.io.*;
  4. import java.net.Socket;
  5. import java.util.Vector;
  6.  
  7. public class NetClient extends TCPClient {
  8.  
  9.     public boolean debug = true;
  10.     private StringBuffer line_buffer = new StringBuffer( 32 );
  11.     
  12.     public String ReadLine() {
  13.         String line;
  14.         
  15.         try {
  16.             int c;
  17.             
  18.             line_buffer.setLength( 0 );
  19.             while ( true ) {
  20.                 c = input.read();
  21.                 if ( c == -1 || c == '\n' || c == '\r' ) {
  22.                     break;
  23.                 }
  24.                 line_buffer.append( (char) c );
  25.             }
  26.             if ( c == '\r' ) {
  27.                 c = input.read(); // skip the \n
  28.             }
  29.             
  30.             line = line_buffer.toString();
  31.             if ( debug ) {
  32.                 System.out.println( ">" + ObscureCommand( line ) );
  33.             }
  34.         } catch( IOException e ) {
  35.             line = null;
  36.         }
  37.         
  38.         return line;
  39.     }
  40.     
  41.     public static int ResponseCode( String line ) {
  42.         int code;
  43.         
  44.         if ( line.length() < 3 ) {
  45.             code = -1;
  46.         } else {
  47.             try {
  48.                 code = Integer.parseInt( line.substring( 0, 3 ) );
  49.             } catch ( NumberFormatException e ) {
  50.                 code = -1;
  51.             }
  52.         }
  53.         
  54.         return code;
  55.     }
  56.     
  57.     public static int ResponseHighCode( int code ) {
  58.         if ( code < 100 || code > 599 ) {
  59.             return -1;
  60.         } else {
  61.             return code / 100;
  62.         }
  63.     }
  64.     
  65.     public String ReadResponse() {
  66.         String line = ReadLine();
  67.         int code = ResponseCode( line );
  68.         if ( code >= 0 && line.length() >= 4 && line.charAt( 3 ) == '-' ) {
  69.             String temp;
  70.             do {
  71.                 temp = ReadLine();
  72.             } while ( temp.length() < 4 || temp.charAt( 3 ) != ' ' || ResponseCode( temp ) != code );
  73.         }
  74.         
  75.         return line;
  76.     }
  77.     
  78.     public int ReadResponseCode() {
  79.         return ResponseCode( ReadResponse() );
  80.     }
  81.     
  82.     public int ReadResponseHighCode() {
  83.         return ResponseHighCode( ResponseCode( ReadResponse() ) );
  84.     }
  85.     
  86.     public void ReadResponseCheck() throws IOException {
  87.         if ( ReadResponseHighCode() != 2 ) {
  88.             throw new NetClientException( "response check failed" );
  89.         }
  90.     }
  91.     
  92.     public Vector ReadResponses() {
  93.         Vector lines = new Vector();
  94.         
  95.         String line = ReadLine();
  96.         lines.addElement( line );
  97.         int code = ResponseCode( line );
  98.         if ( code >= 0 && line.length() >= 4 && line.charAt( 3 ) == '-' ) {
  99.             String temp;
  100.             do {
  101.                 temp = ReadLine();
  102.                 lines.addElement( line );
  103.             } while ( temp.length() < 4 || temp.charAt( 3 ) != ' ' || ResponseCode( temp ) != code );
  104.         }
  105.         
  106.         return lines;
  107.     }
  108.     
  109.     public void SendLineNoFlush( String line ) throws IOException {
  110.         int len = line.length();
  111.         for ( int i = 0; i < len; i++ ) {
  112.             output.write( line.charAt(i) );
  113.         }
  114.         output.write( '\r' );
  115.         output.write( '\n' );
  116.     }
  117.  
  118.     public void SendLine( String line ) throws IOException {
  119.         SendLineNoFlush( line );
  120.         output.flush();
  121.     }
  122.     
  123.     public int SendCommandHighCode( String command ) throws IOException {
  124.         SendLine( command );
  125.         if ( debug ) {
  126.             System.out.println( "<" + ObscureCommand( command ) );
  127.         }
  128.         return ReadResponseHighCode();
  129.     }
  130.     
  131.     public void SendCommandCheck( String command ) throws IOException {
  132.         if ( SendCommandHighCode( command ) != 2 ) {
  133.             throw new NetClientException( "command '"+ ObscureCommand( command ) + "' failed" );
  134.         }
  135.     }
  136.     
  137.     public static String ObscureCommand( String command ) {
  138.         if ( command.regionMatches( true, 0, "PASS", 0, 4 ) ) {
  139.             command = "PASS ******";
  140.         }
  141.         return command;
  142.     }
  143.     
  144. }
  145.  
  146. class NetClientException extends TCPClientException {
  147.     public NetClientException( String s ) {
  148.         super( s );
  149.     }
  150. }
  151.  
  152.